﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using WW.NActiveRecord.Attriutes;
using WW.NActiveRecord.Internal;
using WW.NActiveRecord.Tests.Models;

namespace WW.NActiveRecord.Tests.Associations
{
    [TestFixture]public class TestHasMany:BaseTest
    {

        [TestFixtureSetUp]
        public void SetUpFixture(){
            base.SetUpDb(Table_Suppliers,Table_Products,Table_Categories);
        }

        [Test]
        public void TestForeignKeyParameter() {
            var p=Category.Find(1).Products;
            p.Count().ShouldBe(12);
            p.Command().CommandText.ShouldBe("SELECT Products.* FROM Products WHERE categoryID=@foreignKeyParameter");
            p.Sql().ShouldBe("SELECT Products.* FROM Products WHERE categoryID=1");
        }

        [Test]
        public void TestOptions() {
            var p = Category_Options.Find(1).Products2;
            p.Sql().ShouldBe("SELECT Products.* FROM Products WHERE categoryID=1");
            p.Count().ShouldBe(12);
        }
        [Table(Name = "Categories")]
        public class Category_Options : Base<Category_Options> {
            private readonly MemberSet<Product> products2 = HasMany<Product>(options => {
                options.ForeignKey = "categoryID";
                return options.Set;
            });

            public EntitySet<Product> Products2{
                get { return products2.Value; }
            }
        }
       
        [Test]public void TestOptionsWhere(){
            var p = Category_Options_Where.Find(1).Products2;
            p.Sql().ShouldBe("SELECT Products.* FROM Products WHERE Discontinued='True' AND categoryID=1");
            p.Count().ShouldBe(1);
        }
        [Table(Name = "Categories")]
        public class Category_Options_Where : Base<Category_Options_Where> {
            private readonly MemberSet<Product> products3 = HasMany<Product>(options => {
                options.ForeignKey = "categoryID";
                return options.Set.Where(Discontinued => true);
            });

            public EntitySet<Product> Products2 {
                get { return products3.Value; }
            }
        }

    }
}